home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 176-200 / scopedisk180 / addtools / addtools.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  7KB  |  281 lines

  1. /**********************************************************************/
  2. /*                                                                    */
  3. /*      AddTools - Adds menu items to the Workbench Tools menu.       */
  4. /*                                                                    */
  5. /*                            Version 1.1                             */
  6. /*                                                                    */
  7. /*                          by Steve Tibbett                          */
  8. /*                                                                    */
  9. /**********************************************************************/
  10.  
  11.     // 2.0 includes required
  12.  
  13. #include <exec/types.h>
  14. #include <exec/ports.h>
  15. #include <workbench/workbench.h>
  16. #include <workbench/startup.h>
  17. #include <intuition/intuition.h>
  18. #include <dos/dos.h>
  19. #include <dos/dosextens.h>
  20. #include <dos/dostags.h>
  21. #include <exec/memory.h>
  22. #include <string.h>
  23. #include <proto/wb.h>
  24. #include <proto/dos.h>
  25. #include <proto/intuition.h>
  26. #include <proto/exec.h>
  27.  
  28. struct Library *WorkbenchBase;
  29. struct Library *IntuitionBase;
  30.  
  31. struct AppMenuItem *AppList[32];
  32. BPTR AppListDirs[32];
  33. char *AppListNames[32];
  34. char *AppListCommands[32];
  35. struct Remember *Remember=0;
  36. struct MsgPort *port;
  37. int NumApps=0;
  38.  
  39. /**********************************************************************/
  40. /*                     Lattice C Cleanup Routine                      */
  41. /**********************************************************************/
  42. void
  43. CleanupRoutine(void)
  44. {
  45. while (NumApps)
  46.     {
  47.     NumApps--;
  48.     if (AppListDirs[NumApps])
  49.         UnLock(AppListDirs[NumApps]);
  50.     RemoveAppMenuItem(AppList[NumApps]);
  51.     };
  52.  
  53. if (Remember) FreeRemember(&Remember, TRUE);
  54. if (port) DeletePort(port);
  55. if (WorkbenchBase) CloseLibrary(WorkbenchBase);
  56. if (IntuitionBase) CloseLibrary(IntuitionBase);
  57. }
  58.  
  59.  
  60. /**********************************************************************/
  61. /* Start a command with an output window from a WBArg list.           */
  62. /* The Cmd string that is passed is the template the filenames,       */
  63. /* if any, will be added to.  If there are no arguments, the          */
  64. /* file is invoked directly.  If one argument, it is just appended    */
  65. /* and run (if the Cmd contains %s or %l).  If more than one          */
  66. /* argument, %s will be replaced with one filename and the            */
  67. /* executable will be invoked once for each argument, and a %l will   */
  68. /* be replaced with a list of filenames and the executable invoked    */
  69. /* only once.                                                         */
  70. /**********************************************************************/
  71. void
  72. RunCommandFromWBArgs(char *OrigCmd, BPTR DefDir, long NumWBArgs, struct WBArg *WArgs)
  73. {
  74. BPTR CurDir;
  75. struct Remember *Remember=0;
  76. struct WBArg *WA;
  77. char *CLine, *ExecuteMe, *Cmd, *ConsoleName;
  78. long BufLen=0, NumArgs;
  79. APTR ConsoleTask=0;
  80. static ULONG ConIOTags[] =
  81.     { SYS_Output, 0, SYS_Input, 0, SYS_Asynch, 1, SYS_UserShell, 1, TAG_DONE };
  82. static ULONG NoIOTags[] = 
  83.     { SYS_Asynch, 1, SYS_UserShell, 1, TAG_DONE, };
  84.  
  85. // If only one arg is expected, recursively call until done.
  86. if ((NumWBArgs>1) && (strstr(OrigCmd, "%s")))
  87.     {
  88.     WA=WArgs;
  89.     while (NumWBArgs)
  90.         {
  91.         RunCommandFromWBArgs(OrigCmd, DefDir, 1, WA);
  92.         WA++;
  93.         };
  94.     return;
  95.     };
  96.  
  97. // If %l passed, copy string and replace with %s
  98. if (strstr(OrigCmd, "%l"))
  99.     {
  100.     char *p;
  101.  
  102.     Cmd=(char *)AllocRemember(&Remember, strlen(OrigCmd)+1, MEMF_CLEAR);
  103.     if (Cmd==0) return;
  104.     strcpy(Cmd, OrigCmd);
  105.     p=strstr(Cmd, "%l");
  106.     if (p) p[1]='s';        // change %l to %s
  107.     } else Cmd=OrigCmd;
  108.  
  109. ExecuteMe=Cmd;
  110.  
  111. if (NumWBArgs==0 && strstr(Cmd, "%s"))
  112.     {
  113.     char *p;
  114.     ExecuteMe=(char *)AllocRemember(&Remember, strlen(Cmd)+8, MEMF_CLEAR);
  115.     if (ExecuteMe==0) { FreeRemember(&Remember, TRUE); return; };
  116.     sprintf(ExecuteMe, Cmd, "");
  117.     };
  118.  
  119. if (NumWBArgs && strstr(Cmd, "%s"))
  120.     {
  121.     WA=WArgs;
  122.     NumArgs=NumWBArgs;
  123.  
  124.     BufLen=strlen(Cmd)+4;
  125.  
  126.     while (NumArgs--)
  127.         {
  128.         char PathName[512];
  129.         NameFromLock(WA->wa_Lock, PathName, 512);
  130.         BufLen+=6+strlen(WA->wa_Name)+strlen(PathName);
  131.         WA++;
  132.         };
  133.     
  134.     CLine=(char *)AllocRemember(&Remember, BufLen, MEMF_CLEAR);
  135.     if (CLine)
  136.         {
  137.         WA=WArgs;
  138.         NumArgs=NumWBArgs;
  139.  
  140.         while (NumArgs--)
  141.             {
  142.             char TempBuff[255], c;
  143.             
  144.             strcat(CLine, " \"");
  145.             NameFromLock(WA->wa_Lock, TempBuff, 255);
  146.             strcat(CLine, TempBuff);
  147.             c=CLine[strlen(CLine)-1];
  148.             if (c!=':' && c!='/')
  149.                 strcat(CLine, "/");
  150.             strcat(CLine, WA->wa_Name);
  151.             strcat(CLine, "\"");
  152.             WA++;
  153.             };
  154.  
  155.         ExecuteMe=(char *)AllocRemember(&Remember, strlen(CLine)+strlen(Cmd)+8, MEMF_CLEAR);
  156.         if (ExecuteMe==0) 
  157.  
  158.             {
  159.             FreeRemember(&Remember, TRUE);
  160.             return;
  161.             };
  162.  
  163.         sprintf(ExecuteMe, Cmd, CLine);
  164.         };
  165.     };
  166.  
  167. ConIOTags[1]=0;
  168.  
  169. ConsoleName=(char *)AllocRemember(&Remember, strlen(ExecuteMe)+50, MEMF_CLEAR);
  170. if (ConsoleName)
  171.     {
  172.     char *buff=AllocRemember(&Remember, strlen(Cmd)+1, MEMF_CLEAR);
  173.     
  174.     if (buff)
  175.         {
  176.         sprintf(buff, Cmd, "");
  177.         if (buff[strlen(buff)-1]==32) 
  178.             buff[strlen(buff)-1]=0;
  179.         
  180.         sprintf(ConsoleName, "CON:10/25/550/150/%s Output/AUTO/CLOSE/WAIT", buff);
  181.         ConIOTags[1]=Open(ConsoleName, MODE_NEWFILE);
  182.         if (ConIOTags[1])
  183.             {
  184.             struct Process *Proc=((struct Process *)FindTask(0L));
  185.             struct FileHandle *FH;
  186.  
  187.             ConsoleTask=Proc->pr_ConsoleTask;
  188.             FH=BADDR(ConIOTags[1]);
  189.             SetConsoleTask(FH->fh_Type);
  190.             ConIOTags[3]=Open("*", MODE_OLDFILE);
  191.             };
  192.         };
  193.     };
  194.  
  195. if (DefDir)
  196.     CurDir=CurrentDir(DefDir);
  197.  
  198. if (ConIOTags[1])
  199.      System(ExecuteMe, (ULONG *)&ConIOTags);
  200.     else System(ExecuteMe, (ULONG *)&NoIOTags);
  201.  
  202. if (DefDir)
  203.     CurrentDir(CurDir);
  204.  
  205. if (ConsoleTask)
  206.     SetConsoleTask(ConsoleTask);
  207.  
  208. FreeRemember(&Remember, TRUE);
  209. }
  210.  
  211.  
  212. /**********************************************************************/
  213. /*                        It all happens here                         */
  214. /**********************************************************************/
  215. int
  216. main(void)
  217. {
  218. BPTR FP;
  219. struct AppMessage *msg;
  220.  
  221. onexit(CleanupRoutine);
  222.  
  223. if ((WorkbenchBase = OpenLibrary("workbench.library",36))==0)
  224.     return(0);
  225.  
  226. if ((IntuitionBase = OpenLibrary("intuition.library", 33))==0)
  227.     return(0);
  228.  
  229. if ((port = CreatePort(0,0))==0)
  230.     return(0);
  231.  
  232. if ((FP=(LONG)Open("S:ToolsList", MODE_OLDFILE))==0)
  233.     return(0);
  234.  
  235. while (TRUE)
  236.     {
  237.     char buff[255];
  238.     char otherbuff[255];
  239.     char dirbuff[255];
  240.  
  241.     if (FGets(FP, buff, 255)==0) break;
  242.     if (isspace(buff[0]) || buff[0]==';' || buff[0]=='-') continue;
  243.     if (FGets(FP, otherbuff, 255)==0) break;
  244.     if (FGets(FP, dirbuff, 255)==0) break;
  245.  
  246.     if (buff[0]) buff[strlen(buff)-1]=0;
  247.     if (otherbuff[0]) otherbuff[strlen(otherbuff)-1]=0;
  248.     if (dirbuff[0]) dirbuff[strlen(dirbuff)-1]=0;
  249.  
  250.     AppListNames[NumApps]=(char *)AllocRemember(&Remember, strlen(buff)+1, MEMF_CLEAR);
  251.     AppListCommands[NumApps]=(char *)AllocRemember(&Remember, strlen(otherbuff)+1, MEMF_CLEAR);
  252.     if (AppListCommands[NumApps]==0 || AppListNames[NumApps]==0)
  253.         return(0);
  254.  
  255.     AppListDirs[NumApps]=Lock(dirbuff, ACCESS_READ);
  256.  
  257.     strcpy(AppListCommands[NumApps], otherbuff);
  258.     strcpy(AppListNames[NumApps], buff);
  259.     AppList[NumApps] = (struct AppMenuItem *)AddAppMenuItemA(NumApps,NumApps,AppListNames[NumApps],port,NULL);
  260.     NumApps++;
  261.     }
  262.  
  263. Close(FP);
  264.  
  265. while (TRUE)
  266.     {
  267.     ULONG ID;
  268.  
  269.     Wait((1<<port->mp_SigBit)|SIGBREAKF_CTRL_F);
  270.  
  271.     // If no message arrived, it must be a CTRL_F
  272.  
  273.     if ((msg = (struct AppMessage *) GetMsg(port))==0)
  274.         break;
  275.  
  276.     ID=msg->am_ID;
  277.     RunCommandFromWBArgs(AppListCommands[ID], AppListDirs[ID], msg->am_NumArgs, msg->am_ArgList);
  278.     ReplyMsg((struct Message *) msg);
  279.     }
  280. }
  281.